<?php
/*
 * This file is part of phpep.
 * 
 * phpep
 * 
 * Copyright (C) 2007 Andrés Otárola Alvarado pgugged/cast -- otarola.andres@gmail.com
 * 
 * phpep is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * phpep is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *    
 */
////Clase que contiene los datos de conección
////Lee de un archivo de configuración llamado config.xml

final class DataSource {
	static private $singleton;
	private $user;
	private $pass;
	private $host;
	private $schema;
			 
    private function __construct() {
    	$this->initialize();
    }
    
    private function initialize($configFile = null){
    	if(!$configFile) $configFile = APP_DIR."db/config.xml";
    	$handle = fopen($configFile, "r");
    	$xml_raw = fread($handle, filesize($configFile));
    	fclose($handle);
    	
    	$xml = new SimpleXMLElement($xml_raw);
    	$this->user = trim($xml->user);
    	$this->pass = trim($xml->password);
    	$this->host = trim($xml->host);
    	$this->schema 	= trim($xml->schema);
    }
    ////Metodos getters y setters dinamicos
    function __call($method, $arguments) {
        $prefix = strtolower(substr($method, 0, 3));
        $property = strtolower(substr($method, 3));

        if (empty($prefix) || empty($property)) {
            return;
        }

        if ($prefix == "get" && isset($this->$property)) {
            return $this->$property;
        }

        if ($prefix == "set" && isset($this->$property)) {
        	throw new Exception("Propiedad de solo lectura");
        }
        throw new Exception("Metodo no implementado");
    }
    ////Adopto el patron de diseño "Singleton"
    ////para retornar siempre el mismo objeto, 
    ////lo cual es lógico que solo exista 1 objeto
    ////de tipo de datos de conexion
    static function getInstance(){
    	if(!self::$singleton) {
    		self::$singleton = new DataSource();
    	}
    	return self::$singleton;
    } 
}
?>